<?php
require_once('./Controller.php');
require_once('../models/Content.php');

class CreateController extends Controller{

    public function index()
    {
        $url = '../views/create.html';
        $this->view($url,null);
    }

    public function create($arr)
    {
        $data = [
            'user_id' => $_SESSION['userid'],
            'title' => $arr['title'],
            'content' => $arr['content'],
            'tags' => $arr['tags'],
        ];

        $model = new Content();
        $id = $model->create($data);

        if($id){
            $url = '../controllers/IndexController.php';
            $this->redirect($url);
            exit();
        }else{
            $url = "../controllers/CreateController.php?error=1";
            $this->redirect($url);
            exit();
        }
    }

}

$create = new CreateController();

if(isset($_GET['error']) && $_GET['error'] ==1){
    echo "<script>alert('添加文章失败,请重新添加!');</script>";

}

if(isset($_POST['content']) && !empty($_POST['content'])){
    $create->create($_POST);
}

$create->index();